//
//  YRJDiscountZoneTableViewCell.m
//  YanRenjie
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJDiscountZoneTableViewCell.h"
#import "YRJTripHeader.h"

#define TEMPWIDTH self.frame.size.width
#define TEMPHEIGHT self.frame.size.height
@implementation YRJDiscountZoneTableViewCell
- (void)dealloc {
    [_myCollectionView release];
    [_myFlowLayout release];
    [_tempArray release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell {
    NSLog(@"%f", SHEIGHT);
    NSLog(@"%f", SWIDTH);
    self.myFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.myFlowLayout.itemSize = CGSizeMake((180.0 / 375) * SWIDTH, (180.0 / 667) * SHEIGHT);
    self.myFlowLayout.minimumInteritemSpacing = 0;
    self.myFlowLayout.minimumLineSpacing = 0;
    self.myFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, (10.0 / 667) * SWIDTH);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, (360.0 / 667) * SHEIGHT) collectionViewLayout:self.myFlowLayout];
    [self.contentView addSubview:self.myCollectionView];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView registerClass:[YRJDiscountZoneCollectionViewCell class] forCellWithReuseIdentifier:@"discountZoneReuse"];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myCollectionView.scrollEnabled = NO;
    
    [self.myCollectionView registerClass:[YRJDiscountCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
    [_myCollectionView release];
    [_myFlowLayout release];
}

#pragma mark - collectionView 两个必须实现的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YRJDiscountZoneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discountZoneReuse" forIndexPath:indexPath];
    if (self.tempArray.count != 0) {
        cell.titleLabel.text = [self.tempArray[indexPath.row] title];
        cell.photoImageView.userInteractionEnabled = YES;
        [cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.tempArray[indexPath.row] photo]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        cell.priceLabel.text = [[[self.tempArray[indexPath.row] price] stringByReplacingOccurrencesOfString:@"<em>" withString:@""] stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        cell.priceoffLabel.text = [self.tempArray[indexPath.row] priceoff];
    }
    return cell;
}

#pragma mark 重写tempArray的setter方法
- (void)setTempArray:(NSMutableArray *)tempArray {
    if (_tempArray != tempArray) {
        _tempArray = tempArray;
    }
    [self.myCollectionView reloadData];
}

#pragma mark - collectionView的地步添加一个按钮
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionFooter) {
        YRJDiscountCollectionReusableView *reusableView = [collectionView dequeueReusableCellWithReuseIdentifier:@"footerView" forIndexPath:indexPath];
        reusableView.forMoreButton.backgroundColor = [UIColor redColor];
        return reusableView;
    }
    return nil;
}


#pragma mark - Method  did Select Item At IndexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate pushToDiscountZoneWebView:[self.tempArray[indexPath.row] id]];
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
